local Config = require("Config")
local Util = require("common.Util")
local Json = require("common.Json")
local Lang = require("common.Lang")
local CDkeyRewardConf = require("excel.CDkeyReward").reward
local Msg = require("core.Msg")
local Human = require("core.Human")
local Broadcast = require("broadcast.Broadcast")
local DeliverDefine = require("tencentApi.Define")
local MailLogicFW =require("mail.MailLogicFW")
local Grid = require("bag.Grid")

--激活码返回值
ACTIVATE_OK = 1
ACTIVATE_USED = 2
ACTIVATE_LEN_INVALID = 3
ACTIVATE_NOT_EXIST = 4


--输入参数：code,account,ts
function checkActivate(human, code)
	local s2aParam = {}
	local now = os.time()
    s2aParam.code = code
    s2aParam.ts = now
    s2aParam.account = human.account
    s2aParam.action = "get"
	s2aParam.api_cbMethod = "OnCheckActivateReturn"
	_G.thread_http.send(DeliverDefine.CODE_PHP,Json.Encode(s2aParam))
end

function setActivate(human, code, type)
	local s2aParam = {}
	local now = os.time()
	s2aParam.code = code
	s2aParam.type = type
	s2aParam.ts = now
	s2aParam.account = human.account
	s2aParam.action = "set"
    s2aParam.api_cbMethod = "OnSetActivateReturn"
   _G.thread_http.send(DeliverDefine.CODE_PHP,Json.Encode(s2aParam))
end

local function getDBDataByType(human, type)
	for i = 0, human.db.activateHisLen-1 do
		if human.db.activateHis[i].type == type then
			return human.db.activateHis[i]
		end
	end
end

--返回值： ret,account,code,type,isValid
function OnCheckActivateReturn(oJsonInput)
	
	local respond = {}
	respond.ret = 0
	respond.msg = "OK"
	
	local oJsonInput = oJsonInput.ret
	local account = oJsonInput.account
	local code = oJsonInput.code
	local type = tonumber(oJsonInput.type)
	local isValid = oJsonInput.ret
		
	local human = Human.onlineAccount[account] 
	if not human then 
		respond.ret = 2
		respond.msg = "user no online"
		return respond
	end
	
	if isValid ~= ACTIVATE_OK then
		respond.ret = 3
		respond.msg = "cd key invalid"
		
		local tip = Lang.TENCENT_ACTIVE_INVALID
		if isValid == ACTIVATE_USED then
			tip = Lang.TENCENT_ACTIVE_HAS_USE
		elseif isValid == ACTIVATE_LEN_INVALID or isValid == ACTIVATE_NOT_EXIST then
			tip = Lang.TENCENT_ACTIVE_NOT_CORRECT
		end
		
		Broadcast.sendBroadcast(human, Lang.MOUSE, tip) 
		return respond
	end
	
	if #code == 0 then
		respond.ret = 4
		respond.msg = "code is nil"
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_CD_KEY_INVALID)
		return respond	
	end
	
	local conf = CDkeyRewardConf[type]
	if not conf then
		respond.ret = 5
		respond.msg = "type invalid"
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_ACT_TYPE_KEY_INVALID)
		return respond	
	end

	-- check end date
	if conf.endTime and Util.diffDay(conf.endTime) > 0 then
		respond.ret = 6
		respond.msg = "cd key over time"
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_CD_KEY_OVER_TIME)
		return respond
	end
	
	-- server index
	if conf.serverIndexMin ~= 0 and not (conf.serverIndexMin <= Config.SVR_INDEX and Config.SVR_INDEX  <= conf.serverIndexMax or Config.IS_USE_GM_CMD)  then
		respond.ret = 7
		respond.msg = "server index invalid"
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_SERVER_INDEX_INVALID)
		return respond	
	end
	
	local dbData = getDBDataByType(human, type)
	if dbData then
		if conf.sumCnt ~= 0 and dbData.sumCnt and dbData.sumCnt >= conf.sumCnt then						-- check sum cnt
			respond.ret = 7
			respond.msg = "get cnt over"
			Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_SUM_CNT_INVALID)
			return respond
		end
		
		if Util.diffDay(os.time()) == 0 then
			if conf.dailyCnt ~= 0 and dbData.dailyCnt and dbData.dailyCnt >= conf.dailyCnt then				-- check daily cnt
				respond.ret = 8
				respond.msg = "daily cnt over"
				Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_DAILY_CNT_INVALID)
				return respond 
			end
		end
	end
	
	setActivate(human, code, type)
	
	return respond
end

local mail_grid_list = mail_grid_list or {}
--返回值 ret,account,type,code
function OnSetActivateReturn(oJsonInput)
	local oJsonInput = oJsonInput.ret
	ret = oJsonInput.ret
	
	local respond = {}
	respond.ret = 0
	respond.msg = "OK"
	
	local account = oJsonInput.account
	local human = Human.onlineAccount[account] 
	if human == nil then 
		respond.ret = 2
		respond.msg = "user no online"
		Broadcast.sendBroadcast(human, Lang.MOUSE, "user no online")
		return respond
	end
	
	if ret ~= 1 then
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_SET_ERR)
		return
	end
	
	local type = tonumber(oJsonInput.type) 
	local code = oJsonInput.code


	local dbData = getDBDataByType(human, type)
	if dbData == nil then
		if human.db.activateHisLen >= 20 then
			assert(nil, "activateHisLen  >= 20")
		end
		dbData = human.db.activateHis[human.db.activateHisLen]
		dbData.type = type
		human.db.activateHisLen = human.db.activateHisLen + 1
	end
	
	local conf = CDkeyRewardConf[type]	
	
	if dbData and conf.sumCnt ~= 0 and dbData.sumCnt and dbData.sumCnt >= conf.sumCnt then						-- check sum cnt
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_SUM_CNT_INVALID)
		return
	end
	
	local now = os.time()
	if Util.diffDay(os.time()) ~= 0 then
		dbData.dailyCnt = 0
	end
	
	if conf.dailyCnt ~= 0 then
		local dCnt = 0
		if dbData and dbData.dailyCnt then
			dCnt = dbData.dailyCnt
		end
		
		if dCnt >= conf.dailyCnt then
			Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_DAILY_CNT_INVALID)
			return
		end
	end
	
	dbData.dailyCnt = (dbData.dailyCnt or 0) + 1
	dbData.sumCnt = (dbData.sumCnt or 0) + 1
	dbData.getTime = now
	
	mail_grid_list[1] = conf.grid				
	MailLogicFW.addSystemMessage(human._id, Lang.TENCENT_ACTIVE_MAIL_TITLE, Lang.TENCENT_ACTIVE_MAIL_CONTENT, mail_grid_list)  
	
	--local msgRet = Msg.gc.GC_ACTIVATE_SET
	--msgRet.ret = 1
	--msgRet.msg = "ok"
	--Msg.send(msgRet,human.fd)
	Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_SUCCESS)
	return respond
end

function exchange(human, code)
	if Config.IS_MIDDLE then
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_NOT_GET_IN_MIDDLE)
		return
	end
	
	if #code == 0 then 
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TENCENT_ACTIVE_CODE_NIL)
		return
	end
	
	checkActivate(human, code)
end

function init()
	local endDate = {}
	for _,conf in ipairs(CDkeyRewardConf) do 
		conf.grid = Grid.create(nil, conf.itemID, conf.itemCnt, 1)
		if #conf.endDate ~= 0 then
			endDate.year = conf.endDate[1]
			endDate.month = conf.endDate[2]
			endDate.day = conf.endDate[3] 
			conf.endTime = os.time(endDate)	
		end	
	end
end
